// pages/nearby/nearby.js
import LocationHelper from "../../utils/location.js";

Page({
  data: {
    currentLocation: "",
    currentCategory: "all",
    loading: false,
    showFilterModal: false,
    selectedDistance: 5000,
    selectedRating: 0,
    categories: [
      { id: "all", name: "全部", icon: "🏠" },
      { id: "hospital", name: "医院", icon: "🏥" },
      { id: "shop", name: "宠物店", icon: "🛍️" },
      { id: "grooming", name: "美容", icon: "✂️" },
      { id: "hotel", name: "寄养", icon: "🏨" },
      { id: "park", name: "公园", icon: "🌳" },
    ],
    distanceOptions: [
      { value: 1000, label: "1km内" },
      { value: 3000, label: "3km内" },
      { value: 5000, label: "5km内" },
      { value: 10000, label: "10km内" },
    ],
    ratingOptions: [
      { value: 0, label: "全部" },
      { value: 4, label: "4星以上" },
      { value: 4.5, label: "4.5星以上" },
      { value: 5, label: "5星" },
    ],
    nearbyPlaces: [],
  },

  onLoad() {
    this.getCurrentLocation();
    this.loadNearbyPlaces();
  },

  onPullDownRefresh() {
    this.refreshData();
  },

  // 获取当前位置
  async getCurrentLocation() {
    try {
      const location = await LocationHelper.getCurrentLocation();
      if (location) {
        // 这里应该调用逆地理编码API获取地址
        this.setData({
          currentLocation: "北京市朝阳区",
        });
      }
    } catch (error) {
      console.error("获取位置失败:", error);
      this.setData({
        currentLocation: "位置获取失败",
      });
    }
  },

  // 刷新位置
  refreshLocation() {
    this.getCurrentLocation();
    this.loadNearbyPlaces();
  },

  // 加载附近地点
  loadNearbyPlaces() {
    this.setData({ loading: true });

    // 模拟数据
    const mockPlaces = [
      {
        id: 1,
        name: "爱宠宠物医院",
        image: "/images/hospital1.jpg",
        rating: 4.8,
        tags: ["24小时", "专业医生", "设备齐全"],
        distance: "500m",
        address: "朝阳区建国路88号",
        phone: "010-12345678",
        location: { latitude: 39.9, longitude: 116.4 },
        category: "hospital",
      },
      {
        id: 2,
        name: "萌宠乐园",
        image: "/images/shop1.jpg",
        rating: 4.5,
        tags: ["用品齐全", "价格实惠", "服务周到"],
        distance: "1.2km",
        address: "朝阳区三里屯路19号",
        phone: "010-87654321",
        location: { latitude: 39.9, longitude: 116.4 },
        category: "shop",
      },
      {
        id: 3,
        name: "宠物美容中心",
        image: "/images/grooming1.jpg",
        rating: 4.9,
        tags: ["专业美容", "环境优雅", "预约制"],
        distance: "2.1km",
        address: "朝阳区工体北路8号",
        phone: "010-11223344",
        location: { latitude: 39.9, longitude: 116.4 },
        category: "grooming",
      },
    ];

    setTimeout(() => {
      this.setData({
        nearbyPlaces: mockPlaces,
        loading: false,
      });
      wx.stopPullDownRefresh();
    }, 1000);
  },

  // 刷新数据
  refreshData() {
    this.loadNearbyPlaces();
  },

  // 切换分类
  switchCategory(e) {
    const categoryId = e.currentTarget.dataset.id;
    this.setData({
      currentCategory: categoryId,
    });
    this.loadNearbyPlaces();
  },

  // 显示筛选弹窗
  showFilter() {
    this.setData({
      showFilterModal: true,
    });
  },

  // 关闭筛选弹窗
  closeFilter() {
    this.setData({
      showFilterModal: false,
    });
  },

  // 阻止事件冒泡
  stopPropagation() {
    // 阻止事件冒泡
  },

  // 选择距离
  selectDistance(e) {
    const value = e.currentTarget.dataset.value;
    this.setData({
      selectedDistance: value,
    });
  },

  // 选择评分
  selectRating(e) {
    const value = e.currentTarget.dataset.value;
    this.setData({
      selectedRating: value,
    });
  },

  // 重置筛选
  resetFilter() {
    this.setData({
      selectedDistance: 5000,
      selectedRating: 0,
    });
  },

  // 应用筛选
  applyFilter() {
    this.closeFilter();
    this.loadNearbyPlaces();
  },

  // 打开地点详情
  openPlaceDetail(e) {
    const placeId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/place-detail/place-detail?id=${placeId}`,
    });
  },

  // 拨打电话
  callPlace(e) {
    const phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
      fail: () => {
        wx.showToast({
          title: "拨号失败",
          icon: "none",
        });
      },
    });
  },

  // 打开地图导航
  openMap(e) {
    const location = e.currentTarget.dataset.location;
    wx.openLocation({
      latitude: location.latitude,
      longitude: location.longitude,
      name: "目的地",
      address: "详细地址",
      fail: () => {
        wx.showToast({
          title: "打开地图失败",
          icon: "none",
        });
      },
    });
  },
});
